package com.thirteen.app.service.impl;

import com.thirteen.app.currentConfig.sysConfig;
import com.thirteen.app.dao.ExamDao;
import com.thirteen.app.dao.ExamRecordDao;
import com.thirteen.app.dto.Judge.ExamRecordList;
import com.thirteen.app.dto.PaperWithAnswerResponse.UnJudgedDto;
import com.thirteen.app.dto.JudgeSubjectiveResquest.JudgeByPersonDto;
import com.thirteen.app.entity.Exam;
import com.thirteen.app.service.ExamRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service("examRecordService")
public class ExamRecordServiceImpl implements ExamRecordService {

    @Resource
    private ExamDao examDao;

    @Resource
    private ExamRecordDao examRecordDao;

    //    添加考试记录信息
    @Override
    public Integer insertRecord(Integer examId, Integer userId) {
        Exam exam = examDao.queryById(examId);
        Integer tryCount = examRecordDao.queryTryCount(examId, userId);
        if (tryCount == null) {
            tryCount = 0;
        }
        examRecordDao.insertRecord(examId, userId, exam.getPaperId(), tryCount + 1);
        Integer examRecordId = examRecordDao.queryExamRecordId(examId, userId);
        return examRecordId;
    }

    //    是否可以重新参加该场考试
    public Boolean canRedoExam(Integer examId, Integer userId) {
        if ((examRecordDao.queryTryCount(examId, userId) >= examDao.queryMaxTryCount(examId))) {
            return false;
        }
        return true;
    }

    //    是否可以参加新的考试
    public Boolean canOpenNewExam(Integer examId, Integer userId) {
        if ((sysConfig.hasMoreExam) && (examRecordDao.queryCurrentExamNum(userId) > 0) && (examRecordDao.queryState(examId, userId) == 0)) {
            return false;
        }
        return true;
    }

    //    是否可以浏览答案
    @Override
    public Boolean canBrowseAnswerAndAnalysiis(Integer examRecordId) {
        return null;
    }

    //    是否第一次参加
    @Override
    public Boolean isFirstAttend(Integer examId, Integer userId) {
        return examRecordDao.isFirstAttend(examId, userId) == 0;
    }

    @Override
    public void updateScore(JudgeByPersonDto judgeByPersonDto) {
        examRecordDao.updateScore(judgeByPersonDto);
    }

    @Override
    public List<UnJudgedDto> queryAllUnJudgeByExamId(Integer examId, Integer userId) {
        return examRecordDao.queryAllUnJudgeByExamId(examId,userId);
    }

    // 查询某次考试所有试卷
    @Override
    public List<ExamRecordList> queryAllByExamId(Integer examId,String realName){
        return examRecordDao.queryAllByExamId(examId,realName);
    }


}
